package com.example.neusoftfood_813uk.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.neusoftfood_813uk.R;
import com.example.neusoftfood_813uk.bean.Loginbean;
import com.example.neusoftfood_813uk.listener.LoginListener;
import com.example.neusoftfood_813uk.model.LoginModel;

public class LoginActivity extends AppCompatActivity implements LoginListener {
    private EditText etname,etpass;
    private Button btnlogin,btnregister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        intiViews();
    }

    private void intiViews() {
        etname=findViewById(R.id.etname);
        etpass=findViewById(R.id.etpass);
        btnlogin=findViewById(R.id.button);
        btnregister=findViewById(R.id.button2);

        btnlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });
        btnregister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent2=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent2);
            }
        });
    }

    void login() {
        LoginModel loginModel=new LoginModel();
        loginModel.userLogin(etname.getText().toString().trim(),etpass.getText().toString().trim(),this);
    }

    @Override
    public void onSuccess(Object o, int flag) {
        if(o!=null &&((Loginbean)o).getUserid()!=null)
            Toast.makeText(LoginActivity.this,"登录成功："+((Loginbean)o).getUserid(),Toast.LENGTH_SHORT).show();
        else Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFail() {
        Toast.makeText(LoginActivity.this,"网络错误",Toast.LENGTH_SHORT).show();
    }
}
